var n=0;
var full=0;
var nowNum=0;
var questions=[];
var page=0;
var wxNum;
var kind;
var toquestion=1;
var tologin= function () {
  var that = this;
  wx.showToast({
    title: '登录中',
    icon: 'loading',
    duration: 1000
  })
  wx.login({
    success: function (res) {
      if (res.code) {
        //发起网络请求
        wx.request({
          url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLoginFalv',
          data: {
            code: res.code
          },
          success: function (res) {
            console.log("return" + res.data);
            wx.setStorage({
              key: "login_uuId",
              data: res.data
            });
            getApp().globalData.openid = res.data;
            wxNum = res.data;
           
          }
        })
      } else {
        console.log('获取用户登录态失败！' + res.avatarUrl)
      }
    }
  })
}
Page({
  data:{
    noShowQue:'display',
    showQue:'none',
    questions:[],
    p1:"",
    p2:"",
    kinds:"",
    hidden:true,
    full:"加载中...",
    functions:[],
    img1:"",
    img2:"",
    img3:"",
    smallFun: [
      { moduleDetails: "医院简介", moduleContent: "医院概况 全面了解", icon: '../image/hospital.png' },
      { moduleDetails: "优惠活动", moduleContent: "优惠活动 了解详情", icon: '../image/active.png' },
      { moduleDetails: "专家讲座", moduleContent: '专家讲座 全面学习', icon: '../image/expert_teach.png' },
      { moduleDetails: "预约挂号", moduleContent: "预约挂号 方便服务", icon: "../image/reservation.png" }
    ],
   
    imgtest:[],
  
  },
  chooseSmallFun: function (e) {
    var index = e.currentTarget.id;
    var allFun = this.data.smallFun;
    var that = this;
    var chooseFun = allFun[index].moduleDetails;

    switch (chooseFun) {
      case allFun[0].moduleDetails:
        {
          wx.navigateTo({
            url: '../introduce/introduce?url=https://www.liuxuanlong.xyz/fenda/wx/findHospitalProfile&&hospitalId=' + getApp().globalData.hospitalId + '&&navText= ' + allFun[0].moduleDetails + '',
          })
        }
        break;
      case allFun[1].moduleDetails:
        {
          wx.navigateTo({
            url: '../introduce/introduce?url=https://www.liuxuanlong.xyz/fenda/wx/findHospitalActivity&&hospitalId=' + getApp().globalData.hospitalId + '&&navText=' + allFun[1].moduleDetails + '',
          })
        }
        break;
      case allFun[2].moduleDetails:
        {
          wx.navigateTo({
            url: '../list/list?url=https://www.liuxuanlong.xyz/fenda/wx/findKnowledge&&hospitalId=' + getApp().globalData.hospitalId + '&&navText=' + allFun[2].moduleDetails + '',
          })
        }
        break;
      case allFun[3].moduleDetails:
        {
          wx.navigateTo({
            url: '../kind/kind',
            success: function () {
              tologin();
            },
          })
        }
        break;
      default: {
        console.log('选择功能出错');
      } break;
    }
  },
  whichpage:function(e){                          //判断是否为提问者 选择跳转界面
    var Questions=this.data.questions;
    var index=e.currentTarget.id;
    
    var userNum=Questions[index].user.wxNumber;
    
    if(nowNum!=userNum){
      wx.navigateTo({
        url: 'question/question?id='+Questions[index].id,
      })
    }
    else{
      wx.navigateTo({
        url: '../mine/myquestion/myquestion?id='+Questions[index].id,
      })
    }
  },
  onShareAppMessage: function (res) {
    if (res.from === 'button')
      console.log('页内转发');

  },
  check_black: function () {
    wxNum = wx.getStorageSync('login_uuId');
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/checkUserIsBlack',
      data: {
        wxNumber: wxNum
      },
      success: function (res) {
        console.log(res.data,'拉黑?')
        if (res.data == '是') {
          wx.showModal({
            title: '',
            content: '检测您发表了不正当言论，已限制您进行提问。请联系平台人员已解除限制',
          })
        }
        else {
         
          wx.navigateTo({
            url: '../toquestion/toquestion?kind=' + kind,
            success: function (res) {
              // success
              toquestion = 1;
            },
            fail: function () {
              // fail
            },
            complete: function () {
              // complete
            }
          })
        }
      },

    })


  },
  onPullDownRefresh:function(){
    console.log('下拉了!');
    page=0;
    this.questions();
  },
  prewImg:function(e){
    var index=e.currentTarget.id;
    var url=this.data.questions[index].questionURLSet;
    var imgs=[];
    for(var i=0;i<url.length;i++){
        imgs.push(url[i].imgURL);
    }
    wx.previewImage({
      urls: imgs,
    })
  },
  Login: function () {
    var that = this;

    wx.checkSession({
      success: function () {
        wx.getStorage({
          key: 'login_uuId',
          success: function (res) {

            wxNum = res.data;
            wx.request({
              url: "https://www.liuxuanlong.xyz/fenda/wx/checkLoginTime",
              data:{
                login_uuId:wxNum
              },
              
              success:function(res){
                if(res.data=='error'){
                  that.tologin();
                }
                else{
                 that.check_black();
                }
              },
            })
           
          },
          fail:function(){
            that.tologin();
          },
        })

      },
      fail: function () {
        console.log("fail")
        that.tologin();
      },

    })


    //

  },
   
  tologin: function () {
    var that = this;
    wx.showToast({
      title: '登录中',
      icon: 'loading',
      duration:1000
    })
    wx.login({
      success: function (res) {
        if (res.code) {
          //发起网络请求
          wx.request({
            url: 'https://www.liuxuanlong.xyz/fenda/wx/wxLogin',
            data: {
              code: res.code
            },
            success: function (res) {
              console.log("return" + res.data);
              wx.setStorage({
                key: "login_uuId",
                data: res.data
              });
              getApp().globalData.openid=res.data;
              wxNum = res.data;
              that.check_black();
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.avatarUrl)
        }
      }
    })
  },
  checkLogin:function(){
    this.Login();
  },
  toquestion:function(e){
    if(toquestion==1){
    this.checkLogin();
    toquestion = 0;
   
    }
    
    var index=e.currentTarget.id;
    kind=this.data.functions[index].info;
    
    
  },
  
  onReachBottom:function(){
    this.setData(
      {
        hidden:false
      }
    );
    
    setTimeout(this.addquestion,1000);
    
  },
  addquestion:function(){
    page++;
    var that=this;
      wx.request({
        url: 'https://www.liuxuanlong.xyz/fenda/wx/findQuestion',
        data: { id: getApp().globalData.hospitalId,page:page,page_size:5},
        method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
        // header: {}, // 设置请求的 header
        success: function(res){
          console.log('新数据',res.data);
          var oldquestions=that.data.questions/*wx.getStorageSync('feeds')*/;
          var length=oldquestions.length;
          var second;
          var questions=res.data;
          for (var i = 0; i < questions.length; i++) {
            if (questions[i].questionClass.length == 0)
              questions[i].questionClass = "特问";
            if (questions[i].questionURLSet.length == 0)
              questions[i].imgDisplay = "none";
            else
              questions[i].imgDisplay = "display";


            second = questions[i].answerTime;
            if (second > 60) {
              if (second > 1440) {
                second = parseInt(second / 1440);
                second = second + "天";
              }
              else {
                second = parseInt(second / 60);
                second = second + "小时"
              }
            }
            else {
              second = second + "分钟"
            }
            questions[i].answerTime = second;

          }
          
          oldquestions=oldquestions.concat(questions);
          if(length==oldquestions.length){
            that.setData(
              {
                full:"到底了！"
              }
            );
          }
         
          that.setData(
            {
              questions:oldquestions,
              hidden:true
            }
          );
          
        
          

         /* try {
                wx.setStorageSync('feeds', oldquestions)
                } catch (e) {}*/
        },
        fail: function() {
          that.setData(
            {
              full:"没网了"
            }
          );
          setTimeout(that.failloading,1000);
          // fail
        },
        complete: function() {
          // complete
        }
      })
  },
  failloading:function(){
    this.setData(
      {
        hidden:true
      }
    );
  },
  images:function(data){
    var photoes = data.hospitalImgSet;
    console.log(data.hospitalImgSet)
     this.setData(
       {
         imgtest:photoes
       }
     );
  },
  functions:function(){
    var that=this;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/findHospital',
      data: {
        id: getApp().globalData.hospitalId
        },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        var datas=res.data.hospitalFieldList;
        getApp().globalData.askMoney = res.data.hospitalFieldList[0].askMoney;
        that.setData(
          {
            
            functions: datas
            
          }
        );
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  questions:function(){
    var that=this;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/findQuestion',
      data: {
        id: getApp().globalData.hospitalId,
        page:0,
        page_size:10
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        console.log('初始数据',res.data);
        // success
        page++;
        wx.stopPullDownRefresh();
        console.log(res.data)
        var second;
        var questions=res.data;
        for(var i=0;i<questions.length;i++){
          if (questions[i].questionClass.length==0)
            questions[i].questionClass="特问";
          if (questions[i].questionURLSet.length==0)
            questions[i].imgDisplay="none";
          else
            questions[i].imgDisplay="display";
          
          second = questions[i].answerTime;
            if(second>60){
              if(second>1440){
                second=parseInt(second/1440);
                second=second+"天";
              }
              else{
                second=parseInt(second/60);
                second=second+"小时"
              }
            }
            else{
              second=second+"分钟"
            }
            questions[i].answerTime=second;
            
        }
        that.setData(
          {
            questions:questions,
            NoshowQue:'none',
            showQue:'display'
          }
        );
        /*try {
                wx.setStorageSync('feeds', questions)
                } catch (e) {}*/
                
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })

    
  },
  smallFuns: function () {
    var that = this;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/findHospitalModuleByHospitalId',
      data: {
        hospitalId: getApp().globalData.hospitalId
      },
      success: function (res) {
        var datas = res.data;
        that.setData(
          {
            smallFun: datas
          }
        );
      }
    })
  },
  onLoad:function(){
    
    var that=this;
    wx.request({
      url: 'https://www.liuxuanlong.xyz/fenda/wx/findHospital',
      data: {id:getApp().globalData.hospitalId},
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        that.smallFuns();
        that.images(res.data);
        that.questions();
        that.functions();
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
    
  },
  
  
 
})